package com.myhome.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myhome.config.constant.R;
import com.myhome.domain.EntrustOrder;
import com.myhome.service.EntrustOrderService;
import com.myhome.vo.TradeEntrustOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author QYiXin
 * @Date 2021 11 06 21 20
 **/
@RestController
@Api("委托记录控制器")
@RequestMapping("/entrustOrders")
public class EntrustOrderController {
    @Autowired
    private EntrustOrderService entrustOrderService ;


    @GetMapping
    @ApiOperation(value = "查询用户的委托记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current" ,value = "当前页") ,
            @ApiImplicitParam(name = "size" ,value = "显示的条数") ,
            @ApiImplicitParam(name = "symbol" ,value = "交易对") ,
            @ApiImplicitParam(name = "symbol" ,value = "类型") ,
    })
    public R<Page<EntrustOrder>> findByPage(@ApiIgnore Page<EntrustOrder> page, String symbol , Integer type){
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()) ;
        Page<EntrustOrder> entrustOrderPage = entrustOrderService.findByPage(page,userId,symbol,type) ;
        return R.ok(entrustOrderPage) ;
    }

    @GetMapping("/history/{symbol}")
    @ApiOperation(value = "查询历史的委托单记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页") ,
            @ApiImplicitParam(name = "size",value = "条数") ,
    })
    public R<Page<TradeEntrustOrderVo>> historyEntrustOrder(@ApiIgnore Page<EntrustOrder> page , @PathVariable("symbol") String symbol){
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()) ;
        Page<TradeEntrustOrderVo> pageData = entrustOrderService.getHistoryEntrustOrder(page,symbol,userId) ;
        return R.ok(pageData) ;
    }



    @GetMapping("/{symbol}")
    @ApiOperation(value = "查询未的委托单记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页") ,
            @ApiImplicitParam(name = "size",value = "条数") ,
    })
    public R<Page<TradeEntrustOrderVo>> entrustOrders(@ApiIgnore Page<EntrustOrder> page , @PathVariable("symbol") String symbol){
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()) ;
        Page<TradeEntrustOrderVo> pageData = entrustOrderService.getEntrustOrder(page,symbol,userId) ;
        return R.ok(pageData) ;
    }

    public boolean isIsomorphic(String s, String t) {
        HashMap<Character, Integer> sMap = new HashMap<>();
        HashMap<Character, Integer> tMap = new HashMap<>();
        for (char ch : s.toCharArray()) {
            if (sMap.containsKey(ch)) {
                sMap.put(ch, sMap.get(ch) + 1);
            }else {
                sMap.put(ch, 1);
            }
        }
        for (char ch : t.toCharArray()) {
            if (tMap.containsKey(ch)) {
                tMap.put(ch, tMap.get(ch) + 1);
            }else {
                tMap.put(ch, 1);
            }
        }
        //Collection<Integer> sValues = sMap.values();
        ArrayList<Integer> sValues = new ArrayList<>(sMap.values());
        //Collection<Integer> values = sMap.values();
        int[] sArr = new int[sValues.size()];
        int i = 0;
        for (Integer sValue : sValues) {
            sArr[i++] = sValue;
        }
        ArrayList<Integer> tValues = new ArrayList<>(tMap.values());
        int[] tArr = new int[tValues.size()];
        int j = 0;
        for (Integer tValue : tValues) {
            sArr[j++] = tValue;
        }
        Arrays.sort(sArr);
        Arrays.sort(tArr);
        return Arrays.equals(sArr, tArr);
    }
}
